home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v7n16.arc / TRYQSI.ASM < prev    next >
Assembly Source File  |  1988-09-13  |  7KB  |  203 lines

  1.         name    tryqsi
  2.         title   TRYQSI Demo of Integer Quicksort
  3.         page    55,132
  4. ;
  5. ; TRYQSI.ASM    Demonstration of Integer Quicksort
  6. ;
  7. ; Copyright 1988, Ziff Communications Co.
  8. ; PC Magazine * Ray Duncan
  9. ;
  10.  
  11. stdin   equ     0               ; standard input handle
  12. stdout  equ     1               ; standard output handle
  13. stderr  equ     2               ; standard error handle
  14.  
  15. cr      equ     0dh             ; ASCII carriage return
  16. lf      equ     0ah             ; ASCII line feed
  17.  
  18. _TEXT   segment word public 'CODE'
  19.  
  20.         assume  cs:_TEXT,ds:_DATA
  21.  
  22.         extrn   itoa:near
  23.         extrn   atoi:near
  24.         extrn   qsorti:near
  25.  
  26. main    proc    near
  27.  
  28.         mov     ax,_DATA        ; make our data segment
  29.         mov     ds,ax           ; addressable...
  30.         mov     es,ax
  31.  
  32. main1:                          ; begin entry of data
  33.  
  34.         mov     word ptr ix1,0  ; initialize array index
  35.         
  36.                                 ; display "Enter Numbers"
  37.         mov     dx,offset msg1  ; DS:DX = message address
  38.         mov     cx,msg1_len     ; CX = message length
  39.         mov     bx,stdout       ; BX = handle
  40.         mov     ah,40h          ; Fxn 40H = write
  41.         int     21h             ; transfer to MS-DOS
  42.  
  43. main2:                          ; convert item number
  44.                                 ; to ASCII string...
  45.         mov     ax,ix1          ; 1-based item number
  46.         inc     ax
  47.         mov     bx,offset msg3a ; address for string
  48.         call    b2dec           ; convert it
  49.  
  50.                                 ; display item number
  51.         mov     dx,offset msg3  ; DS:DX = message address
  52.         mov     cx,msg3_len     ; CX = length
  53.         mov     bx,stdout       ; BX = handle
  54.         mov     ah,40h          ; Fxn 40H = write
  55.         int     21h             ; transfer to MS-DOS
  56.  
  57.                                 ; read keyboard entry
  58.         mov     dx,offset ibuff ; DS:DX = input buffer
  59.         mov     cx,80           ; CX = max input length
  60.         mov     bx,stdin        ; BX = handle
  61.         mov     ah,3fh          ; Fxn 3FH = read
  62.         int     21h             ; transfer to MS-DOS
  63.  
  64.         cmp     ax,2            ; was anything entered?
  65.         je      main3           ; empty line, exit
  66.  
  67.         mov     si,offset ibuff ; convert input to
  68.         call    atoi            ; binary in reg. AX
  69.                                         
  70.         mov     bx,ix1          ; put data into array
  71.         shl     bx,1            ; (item number * 2)
  72.         mov     [bx+items],ax
  73.  
  74.         inc     word ptr ix1    ; count items stored
  75.         cmp     word ptr ix1,25 ; force maximum = 25
  76.  
  77.         jne     main2           ; get another entry
  78.  
  79. main3:                          ; empty line entered...
  80.  
  81.         cmp     word ptr ix1,0  ; any data in array?
  82.         je      main5           ; no, just exit
  83.  
  84.                                 ; otherwise sort data...
  85.         mov     si,offset items ; DS:SI = first item
  86.         mov     di,ix1          ; DS:DI = last item
  87.         dec     di
  88.         shl     di,1
  89.         add     di,si
  90.         call    qsorti          ; call QuickSort
  91.  
  92.                                 ; display sorted data...
  93.  
  94.         mov     word ptr ix2,0  ; initialize array index
  95.  
  96.                                 ; display "Here are the
  97.                                 ; sorted numbers..."
  98.         mov     dx,offset msg2  ; DS:DX = message address
  99.         mov     cx,msg2_len     ; CX = message length
  100.         mov     bx,stdout       ; BX = handle
  101.         mov     ah,40h          ; Fxn 40H = write
  102.         int     21h             ; transfer to MS-DOS
  103.  
  104. main4:                          ; display next item...
  105.  
  106.                                 ; convert item number
  107.                                 ; to ASCII string
  108.         mov     ax,ix2          ; 1-based item number
  109.         inc     ax              
  110.         mov     bx,offset msg3a ; address for string
  111.         call    b2dec           ; convert it
  112.  
  113.                                 ; display item number
  114.         mov     dx,offset msg3  ; DS:DX = message address
  115.         mov     cx,msg3_len     ; CX = message length
  116.         mov     bx,stdout       ; BX = handle
  117.         mov     ah,40h          ; Fxn 40H = write
  118.         int     21h             ; transfer to MS-DOS
  119.  
  120.         mov     bx,ix2          ; calc. array offset
  121.         shl     bx,1
  122.         mov     ax,[bx+items]   ; get data from array
  123.  
  124.                                 ; convert data to ASCII
  125.         mov     cx,10           ; use base 10
  126.         mov     si,offset obuff ; address for string
  127.         call    itoa            ; convert it
  128.  
  129.                                 ; display converted data...
  130.         mov     cx,ax           ; CX = string length
  131.         mov     dx,si           ; DS:DX = string address 
  132.         mov     bx,stdout       ; BX = handle
  133.         mov     ah,40h          ; Fxn 40H = write
  134.         int     21h             ; transfer to MS-DOS
  135.  
  136.         inc     word ptr ix2    ; advance through array
  137.  
  138.         mov     ax,ix2          ; done with array yet?
  139.         cmp     ax,ix1
  140.         jne     main4           ; no, display another
  141.  
  142.         jmp     main1           ; restart user entry
  143.  
  144. main5:  mov     ax,4c00h        ; final exit to MS-DOS
  145.         int     21h
  146.  
  147. main    endp
  148.  
  149.  
  150. b2dec   proc    near            ; convert binary value 0-99
  151.                                 ;  to decimal ASCII chars.
  152.                                 ; call with 
  153.                                 ; AL = binary data
  154.                                 ; BX = addr. for 2 chars.
  155.         
  156.         aam                     ; divide AL by 10, leaving
  157.                                 ; AH=quotient, AL=remainder
  158.         add     ax,'00'         ; convert to ASCII
  159.         mov     [bx],ah         ; store ten's digit
  160.         mov     [bx+1],al       ; store one's digit
  161.         ret                     ; return to caller
  162.  
  163. b2dec   endp
  164.  
  165. _TEXT   ends
  166.  
  167.  
  168. _DATA   segment word public 'DATA'
  169.  
  170. msg1    db      cr,lf,lf
  171.         db      'Enter numbers to sort...'
  172.         db      cr,lf
  173. msg1_len equ $-msg1
  174.  
  175. msg2    db      cr,lf
  176.         db      'Here are the sorted numbers...'
  177.         db      cr,lf
  178. msg2_len equ $-msg2
  179.  
  180. msg3    db      cr,lf,'Item '
  181. msg3a   db      'xx: '
  182. msg3_len equ $-msg3
  183.  
  184. ibuff   db      80 dup (?)      ; keyboard input buffer
  185.  
  186. obuff   db      80 dup (?)      ; output conversion buffer
  187.  
  188. items   dw      25 dup (0)      ; holds numbers to sort
  189.  
  190. ix1     dw      0               ; number of array items 
  191. ix2     dw      0               ; array output pointer
  192.  
  193. _DATA   ends
  194.  
  195.  
  196. STACK   segment para stack 'STACK'
  197.         
  198.         db      128 dup (?)
  199.  
  200. STACK   ends
  201.  
  202.         end     main
  203.